# https://oapi.dingtalk.com/robot/send?access_token=fe51c3ebbacee696c300210610eba7ec5f20c20c0199e70b3c1a1fc19d292c49

import json
import requests
import time
import hmac
import hashlib
import base64
import urllib.parse

def message(content,webhook,secre):
    timestamp = str(round(time.time() * 1000))
    secret = 'SEC22cf798f3e28ab4d822add8af6b5319f41d28b7d650330a52842d3f087dd8f09'
    secret_enc = secret.encode('utf-8')
    string_to_sign = '{}\n{}'.format(timestamp, secret)
    string_to_sign_enc = string_to_sign.encode('utf-8')
    hmac_code = hmac.new(secret_enc, string_to_sign_enc, digestmod=hashlib.sha256).digest()
    sign = urllib.parse.quote_plus(base64.b64encode(hmac_code))

    time_now = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())

    url =webhook + '&timestamp='+ timestamp +'&sign=' + sign
    message = {
        "msgtype": "markdown",
        "markdown": {
            "title": "数据处理完成",
            "text": "#### "+ content +" \n >\n> ![screenshot](http://www.rzccc.com/u/cms/www/201410/140906517nhe.jpg)\n> ###### " + time_now+"推送 \n",
        },
        "isAtAll": True        #是否@所有人，默认否
    }
    headers = {
        'Content-Type': 'application/json',
        "Charset": "UTF-8"
    }
    requests.post(url=url,data=json.dumps(message),headers=headers)

if __name__ == "__main__":
    for i in range(1):
        message(str(i) + "消息: 测试状态！")

